local skel = fk.CreateSkill {
  name = "lb__duanhai",
  tags = { Skill.Compulsory },
  derived_piles = "$lb__duanhai",
}

Fk:loadTranslationTable {
  ["lb__duanhai"] = "断海",
  [":lb__duanhai"] = "<b>锁定技</b>，游戏开始时或每轮结束时，若你没有「<b>断海</b>」牌，你将牌堆平分为两组，选择其中一组扣置为「<b>断海</b>」（首张「<b>断海</b>」牌明置）；其他角色一次性摸两张牌时，改为摸一张牌并获得首张「<b>断海</b>」牌。",

  ["$lb__duanhai"] = "断海",
  ["@lb__duanhai"] = "断海",

  ["#lb__duanhai_top"] = "上：%arg张",
  ["#lb__duanhai_bottom"] = "下：%arg张",
  ["#lb__duanhai"] = "断海：请选择一份牌堆为「<b>断海</b>」",

  ["$lb__duanhai1"] = "潜鳞已现。",
  ["$lb__duanhai2"] = "天洪奔涌。",
}

local duanhai_sepc = {
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(skel.name) and #player:getPile("$lb__duanhai") == 0
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    if #room.draw_pile < 2 then
      room:shuffleDrawPile()
    end
    local top, bottom = {}, {}
    for i, id in ipairs(room.draw_pile) do
      if i < (#room.draw_pile // 2 + 1) then
        table.insert(top, id)
      else
        table.insert(bottom, id)
      end
    end
    local list = { "#lb__duanhai_top:::" .. #top, "#lb__duanhai_bottom:::" .. #bottom }
    local choice = room:askToChoice(player, {
      choices = list,
      skill_name = skel.name,
      prompt = "#lb__duanhai",
    })
    event:setCostData(self, { cards = (table.indexOf(list, choice) == 1 and top or bottom) })
    return true
  end,
  on_use = function(self, event, target, player, data)
    player:addToPile("$lb__duanhai", event:getCostData(self).cards, false, skel.name, player, {})
  end,
}

skel:addEffect(fk.GameStart, duanhai_sepc)

skel:addEffect(fk.RoundEnd, duanhai_sepc)

skel:addEffect("visibility", {
  card_visible = function(self, player, card)
    local owner = table.find(Fk:currentRoom().alive_players, function(to)
      return to:getPileNameOfId(card.id) == "$lb__duanhai"
    end)
    if owner then
      return table.indexOf(owner:getPile("$lb__duanhai"), card.id) == 1
    end
  end,
})

skel:addEffect(fk.BeforeDrawCard, {
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    return target ~= player and player:hasSkill(skel.name) and data.num == 2 and #player:getPile("$lb__duanhai") > 0
  end,
  on_use = function(self, event, target, player, data)
    data.num = 1
    --player.room:obtainCard(target, player:getPile("$lb__duanhai")[1], true, fk.ReasonPrey, target, skel.name)
    player.room:moveCardTo(player:getPile("$lb__duanhai")[1], Card.PlayerHand, target, fk.ReasonPrey, skel.name, "$lb__duanhai", true, target)
  end,
})

skel:addEffect(fk.AfterCardsMove, {
  can_refresh = function(self, event, target, player, data)
    if not player:hasSkill(skel.name, true) then return false end
    for _, move in ipairs(data) do
      return (move.from == player or move.to == player) and move.specialName == "$lb__duanhai"
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local ids = player:getPile("$lb__duanhai")
    local room = player.room
    if #ids > 0 then
      local card = Fk:getCardById(ids[1])
      room:setPlayerMark(player, "@lb__duanhai", card:toLogString())
    else
      room:setPlayerMark(player, "@lb__duanhai", 0)
    end
  end,
})

return skel
